
let proPath = process.argv.splice(2)[0];

console.log(process.cwd());
console.log(proPath);

const fs = require("fs");

function dealWithFunc(regStr, tmpData) {
    let n;
    let targetData = "";
    while (1) {
        n = tmpData.search(regStr);
        if (n < 0) {
            targetData = targetData + tmpData;
            break;
        }

        var preCode = tmpData.substr(0, n + 1); // =号前面的代码部分
        var afterCode = tmpData.substr(n + 1); // 从=号开始的代码部分
        let stack = 0;
        let endpos = 0;
        let tchar;
        let fStartIndex = -1;
        for (var i = 0; i < afterCode.length; ++i) {
            endpos = i + 1;
            tchar = afterCode[i];
            if (tchar == '{') {
                stack = stack + 1
                if (fStartIndex == -1) {
                    fStartIndex = i; // 函数体起始位置
                }
            } else if (tchar == '}') {
                stack = stack - 1 // 函数体结束位置
            }
            if (stack == 0 && fStartIndex != -1) {
                break;
            }
        }

        if (fStartIndex != -1) {
            // 函数体
            let fbody_head = afterCode.substr(0, fStartIndex); // {号前的代码
            let fbody_tail = afterCode.substr(fStartIndex, endpos - fStartIndex); // {到}的代码

            // 为了简单起见，不处理包含function的代码
            if (fbody_tail.search("function") < 0) {
                //================压缩call


                //================压缩func
                var retArr = fbody_tail.match(/([a-zA-Z_\$][\w\$]*\.)+[a-zA-Z_\$][\w\$]*( *\()/g); // 查找func次数
                var funcDic = {}
                if (retArr) {
                    retArr.forEach((str, i) => {
                        let funcStr = str.replace(/ *\(/g, "") //去掉左括号
                        let funcArr = funcDic[funcStr] || (funcDic[funcStr] = [])
                        funcArr.push(funcStr)
                    })
                }
                let funcIdx = 0
                for (var funcStr in funcDic) {
                    let funcArr = funcDic[funcStr]
                    if (funcArr.length > 2) { // 如果func超过2次，则进行转换
                        for (var i = funcArr.length; --i;) {
                            fbody_tail = fbody_tail.replace(funcStr, `_fn_${funcIdx}`); // 转换func成_fn_+funcIdx
                        }
                        fbody_tail = fbody_tail.substr(1, fbody_tail.length - 1);
                        fbody_tail = `{\n\tvar _fn_${funcIdx}=${funcStr};${fbody_tail}`;
                        funcIdx++
                    }
                }

                //================压缩this
                var retArr = fbody_tail.match(/[^\w$]this[^\w]/g); // 查找this次数
                if (retArr && retArr.length > 3) { // 如果this超过3次，则进行转换
                    for (var tIndex = 0; tIndex < retArr.length; ++tIndex) {
                        fbody_tail = fbody_tail.replace(retArr[tIndex], retArr[tIndex][0] + "_this_" + retArr[tIndex][5]); // 转换this成_this_
                    }
                    fbody_tail = fbody_tail.substr(1, fbody_tail.length - 1);
                    fbody_tail = "{\n\tvar _this_=this;" + fbody_tail;
                }
            }

            targetData = targetData + preCode + fbody_head + fbody_tail;
            tmpData = tmpData.substr(n + 1 + endpos); // 从函数体结束后开始

        } else {
            targetData = targetData + tmpData;
            break;
        }

    }
    return targetData;
}


function CompressFuncCode(srcPath, desPath) {
    console.log("开始压缩", srcPath);
    let data = fs.readFileSync(srcPath, "utf8");
    var tmpData = data;
    tmpData = dealWithFunc(/[^$][:=]function\([\w,]*\){/, tmpData);
    let targetData = dealWithFunc(/[^\w]function [\w]*\([\w,]*\){/, tmpData);
    // 输出到文件
    fs.writeFileSync(desPath, targetData);
}

let st_time = Date.now();
// CompressFuncCode(proPath + "bin\\h5\\js\\txitech\\exmod.js", proPath + "bin\\h5\\js\\txitech\\exmod.js");
// CompressFuncCode(proPath + "bin\\h5\\js\\txitech\\gamecore.js", proPath + "bin\\h5\\js\\txitech\\gamecore.js");
// CompressFuncCode(proPath + "bin\\h5\\Index.max.js", proPath + "bin\\h5\\Index.max.js");
// CompressFuncCode(proPath + "bin\\h5\\js\\txdata.js", proPath + "bin\\h5\\js\\txdata.js");
CompressFuncCode("test.js", "test_c.js");
console.log("func压缩耗时:" + (Date.now() - st_time) + "ms");